from django.http import HttpResponse, HttpResponseRedirect
from jieblog.applications import models
from jieblog.applications import bforms
from django.shortcuts import render_to_response
from google.appengine.api import users
from google.appengine.ext import db

def view_tag(request,post_tag):
	all_posts = models.Post.all()
	tag_posts = []
	for post in all_posts:
		if post_tag in post.tags:
			tag_posts.append(post)
	payload = dict(tag_posts = tag_posts)
	return render('tag.html',payload)
			

def feeds(request):
	posts = models.Post.all()
	latest_post = posts[posts.count()-1]
	posts.order('-post_on')
	payload = dict(posts = posts,latest_post = latest_post)
	return render('atom.xml',payload)
	
#	posts_latest = models.Post.fetch()
#	payload = dict(posts = posts)
#	return render('atom.xml',payload)

def render(template, payload):
	return render_to_response(template, payload)
		
def index_page(request,page):
	page = int(page)
	posts_per_page = 4
	show_prev = False
	show_next = False
	all_posts = models.Post.all()
	max_page = (all_posts.count()-1) / posts_per_page
	posts = all_posts.order('-post_on').fetch(posts_per_page, offset = page * posts_per_page)
	show_prev = not (page == 0)
	show_next = not (page == max_page)
	if not posts:
		show_prev = False
		show_next = False
	payload = dict(posts = posts,show_prev = show_prev,show_next = show_next,show_page_panel = show_prev or show_next,prev = page - 1,next = page + 1,max_page = max_page)
	return render('index.html', payload)
	

def index(request):
	user = users.get_current_user()
	if user:
		greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
				  (user.nickname(), users.create_logout_url("/")))
	else:
		greeting = ("<a href=\"%s\">Sign in or register</a>." %
				  users.create_login_url("/"))
	page = 0
	posts_per_page = 4
	show_prev = False
	show_next = False
	all_posts = models.Post.all()
	max_page = (all_posts.count()-1) / posts_per_page
	posts = all_posts.order('-post_on').fetch(posts_per_page, offset = page * posts_per_page)
	show_prev = not (page == 0)
	show_next = not (page == max_page)
	if not posts:
		show_prev = False
		show_next = False

	payload = dict(greeting=greeting,posts = posts,show_prev = show_prev,show_next = show_next,show_page_panel = show_prev or show_next,prev = page - 1,next = page + 1,max_page = max_page)
	return render('index.html', payload)
	
def create(request):
	user = users.get_current_user()
	if users.is_current_user_admin():
		if request.method == 'POST':
			postform = bforms.PostForm(request.POST)
			if postform.is_valid():
				post = postform.save(user)
				return HttpResponseRedirect('/')
		else:
			postform = bforms.PostForm()
	else:
		return HttpResponseRedirect('/')		
	payload = dict(postform = postform)
	return render('full.html', payload)
#	return render_to_response('create.html',{'postform':postform})

def comment(request, post_id):
	post = models.Post.get_by_id(int(post_id))
	comments = models.Comments.all().filter("post",post)
	if request.method == 'POST':
		commentform = bforms.CommentForm(request.POST)
		if commentform.is_valid():				
			comment = commentform.save(post)
			return HttpResponseRedirect('/post/%s'%post_id)
	else:
		commentform = bforms.CommentForm()
	payload = dict(post=post,commentform=commentform,comments=comments)
	return render('comment.html', payload)